<?php

/**
 * ==============================================
 * Created by Finger Technology.
 * Author: ZhiHuaWei <zhihua_wei@foxmail.com>
 * Date: 2019/8/23 14:39
 * Project: YunAdmin
 * Power: YunAdmin应用公共文件
 * Version: 1.0.0
 * ==============================================
 */

use app\admin\model\AdminUser;
use think\Db;

/**
 * 记录后台操作日志
 * @param int $admin_uid
 * @param string $username
 * @param string $url
 * @param string $log_note
 */
function takeOperateLog($admin_uid = 0, $username = '', $url = '', $log_note = '')
{
    $log = [
        'admin_uid' => $admin_uid,
        'username' => empty($username) ? '' : $username,
        'url' => getHttpProtocol() . '://' . $_SERVER['SERVER_NAME'] . $url,
        'param' => base64_encode(json_encode($_REQUEST)),
        'ip' => getClientServerIp(),
        'log_note' => empty($log_note) ? '' : $log_note,
    ];
    Db::name('admin_operate_log')->insert($log);
}

/**
 * 密码加密
 * @param $password
 * @return string
 */
function PWD($password)
{
    return md5(crypt('Y' . $password . 'W', 'Yun'));
}

/**
 * 获得随机字符串
 * @param int $length 长度
 * @param string $type 类型
 * @param int $convert 转换大小写
 * @return string 随机字符串
 */
function getRandomString($length = 6, $type = 'string', $convert = 0)
{
    $config = array('number' => '1234567890', 'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789', 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');

    if (!isset($config[$type]))
        $type = 'string';
    $string = $config[$type];

    $code = '';
    $str_len = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $code .= $string{mt_rand(0, $str_len)};
    }
    if (!empty($convert)) {
        $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
    }
    return $code;
}

/**
 * 获得Http协议
 * @return string
 */
function getHttpProtocol()
{
    $res = array_key_exists('REQUEST_SCHEME', $_SERVER);
    if ($res) {
        $protocol = $_SERVER['REQUEST_SCHEME'];
    } else {
        $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === false ? 'http' : 'https';
    }
    return $protocol;
}

/**
 * 检查管理员用户名是否重复
 * @param int $admin_uid
 * @param $username
 * @return bool
 */
function checkUsernameRepeat($admin_uid = 0, $username)
{
    if ($admin_uid) {
        $check = Db::name('admin_user')->field('admin_uid,username')->where("admin_uid != {$admin_uid} AND username = '{$username}' AND is_delete = 0")->find();
    } else {
        $check = AdminUser::get(['username' => $username, 'is_delete' => 0]);
    }
    if (empty($check)) {
        return false;
    } else {
        return true;
    }
}


function mSubStr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists("mb_substr")) {
        if ($suffix)
            return mb_substr($str, $start, $length, $charset) . "...";
        else
            return mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        if ($suffix)
            return iconv_substr($str, $start, $length, $charset) . "...";
        else
            return iconv_substr($str, $start, $length, $charset);
    }
    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef]
                  [x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    if ($suffix) return $slice . "…";
    return $slice;
}


